<?php
    use lib\Task;
    use lib\Report;
use lib\LocalRedis;
    
    include __DIR__ . '/autoload.php';
    
    $server = new Swoole\Http\Server('0.0.0.0', 7777);
    
    $server->set([
    	'task_enable_coroutine' => true,
        'cpu_affinity_ignore' => [0],
        'worker_num' => 1,
        'task_worker_num' => 1
    ]);
    
    $server->on('request', function ($request, $response) use($server) {
    	$pathInfo = $request->server['path_info'];
    	$pathInfo = trim($pathInfo, '/');
    	$pathInfo = explode('/', $pathInfo);
    	
    	$dst = !empty($pathInfo[0]) ? $pathInfo[0] : '';
    	$action = !empty($pathInfo[1]) ? $pathInfo[1] : '';
    	if(!in_array($dst, ['tboxmanager', 'sendGateway'])) {
    	    $response->end('fail');
    	    return;
    	}
    	
    	
    	$params = $request->rawContent();
    	$params = json_decode($params, true);
    	
    	if($params === null){
    		$response->end('fail');
    		return;
    	}
    	
    	$taskData = ['params' => $params, 'dst' => $dst, 'action' => $action];
    	
    	if($dst === 'tboxmanager') {
    	    $repData = ['statuscode' => 200];
    	    if(isset($params['deviceId'])) {
    	        $repData['deviceId'] = $params['deviceId'];
    	    }
    	    if(isset($params['socketNo'])) {
    	        $repData['socketNo'] = $params['socketNo'];
    	    }
    	    $response->end(json_encode($repData));
    	}else{ //平台发给网关的
    	    $taskData['fd'] = $response->fd;
    	    $response->detach();
    	}
    	
    	$server->task($taskData);
    });
        
        
        
    $server->on('WorkerStart', function ($server, $worker_id) {
    
            
    });
            
    $server->on('Task', function ($serv, \Swoole\Server\Task $task) {
    	$data = $task->data;
    	$dst = $data['dst'];
    	$action = $data['action'];
    	$params = $data['params'];
    	
    	if($dst === 'sendGateway'){ //平台发给网关的
    	    
    	    if(isset($params['deviceId'])) {
    	        $params['simId'] = $params['deviceId'];
    	        $params['deviceId'] = bin2hex($params['deviceId']);
    	    }
    	    
    	    $body = Task::$action($params);
    		$resp = \Swoole\Http\Response::create($data['fd']);
    		
    		$resp->end($body);
    		
    		if(isset($params['orderId'])) { //订单类
    		    if(!is_array($body)) {
    		        $body = json_decode($body, true);
    		    }
    		    
    		    if(!empty($body) && $body['statuscode'] == 200) {
    		        $task->finish(['success' => true, 'action' => $action, 'simId' => $params['simId'], 'portNo' => $params['portNo'], 'orderId' => $params['orderId']]);
    		        return true;
    		    }
    		    
    		}
    		
    	}elseif($dst === 'tboxmanager') { //网关发给平台的
    	    if(isset($params['deviceId'])) {
    	        $params['deviceId'] = hex2bin($params['deviceId']);
    	    }
    		Report::$action($params);
    	}
    	
    	$task->finish(['success' => true]);
    	
    	
    });
                
    $server->on('Finish', function ($serv, $task_id, $data) {
        var_dump('Finish');
        if(isset($data['orderId'])) {
            go(function() use($data) {
                try {
                    $key = $data['simId'] . ':' . $data['portNo'];
                    if($data['action'] === 'addCharge') { //扫码增加充电
                        $key = 'extend:' . $key;
                    }elseif($data['action'] === 'onlineCardCharge') { //线上卡充电
                        $key = 'online_card:' . $key;
                    }
                    $localRedis = new LocalRedis();
                    $localRedis->setex($key, $data['orderId'], 24 * 3600);
                    $localRedis->close();
                } catch (Exception $e) {
                }
            });
        }
    	return true;
    });
                    
    $server->start();
	
	
	
	
	